<?php

class Opis_model extends CI_Model {

    function  __construct() {
        parent::__construct();
        $this->tableName = "mis_opis";
        $this->load->model("functions_model");
    }

    function newOpi($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid){
        $data['tid']=$tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }
     function  getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select("company_name, business_type,relations_mgr,awards_won,added_by,date_added,yr_assessed,champions_trained,training_yr");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $list = $this->consolidate_model->getClients($row['company_name']);
            if (count($list) > 0) {
                $row['company_name'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getBusinessTypes($row['business_type']);
            if (count($list) > 0) {
                $row['business_type'] = $list[0]->name;
            }
            $result[] = $row;
        }
        return $result;
    }

    function getAll($username) {
        $data['added_by'] = $username;
        $this->db->select("date_added, tid,company_name, business_type,yr_assessed,champions_trained,training_yr,awards_won,relations_mgr");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $list = $this->consolidate_model->getClients($row['company_name']);
            if (count($list) > 0) {
                $row['company_name'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getBusinessTypes($row['business_type']);
            if (count($list) > 0) {
                $row['business_type'] = $list[0]->name;
            }
            $row['more'] = "<a href='javascript:loadUrl(\"#opis_result\",\"" . base_url() . "index.php?/more_info/opisMoreInfo/" . $row['tid'] . "\")''>More details</a>";
            unset ($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

     function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("relations_mgr", $search);
                $this->db->or_like("awards_won", $search);
                $this->db->or_like("added_by", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select("date_added, tid, company_name, business_type,yr_assessed,champions_trained,training_yr,awards_won,relations_mgr");
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $tid = $row['tid'];
           $list = $this->consolidate_model->getClients($row['company_name']);
            if (count($list) > 0) {
                $row['company_name'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getBusinessTypes($row['business_type']);
            if (count($list) > 0) {
                $row['business_type'] = $list[0]->name;
            }
            $row['more'] = "<a href='javascript:loadUrl(\"#opis_result\",\"" . base_url() . "index.php?/more_info/opisMoreInfo/" . $row['tid'] . "\")''>More details</>  ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].="  <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteOpi/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
           unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
